﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierExam;
using Yipusoft.SoldierExam.BLL;
using Yipusoft.SoldierExam.Model;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;

public partial class Aptitude_InsertExam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindGrid(PageIndex);
    }

    private void BindGrid(int pageIndex)
    {
        ListPageContral1.ItemCount = AptitudeExam.getCount(lblwhere.Text);
        gvExamSubject.DataSource = AptitudeExam.getList(gvExamSubject.PageSize, pageIndex, lblwhere.Text);
        gvExamSubject.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        #region 权限
        bool isPermission = false;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
        {
            isPermission = true;
        }
        else
        {

            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300305"))
            {
                isPermission = true;
            }

        }
        if (!isPermission)
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        #endregion

        string Where = "";
        if (ddlCount.SelectedValue != "")
        {
            if (Where != "")
            {
                Where += " and ";
            }
            Where = " ExamCount=" + ddlCount.SelectedValue;
        }

        if (txtYear.Text.Trim() != "")
        {
            if (Where != "")
            {
                Where += " and ";
            }
            Where += " ExamYear=" + txtYear.Text;
        }

        if (txtSubject.Text.Trim() != "")
        {
            if (Where != "")
            {
                Where += " and ";
            }
            Where += " AptitudeExamSubject like '%" + txtSubject.Text + "%'";
        }
        lblwhere.Text = Where;
        ListPageContral1.SetFirstPage();
        BindGrid(0);
    }
    protected void gvExamSubject_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (AptitudeExam.DeleteAptitudeExam((int)gvExamSubject.DataKeys[e.RowIndex].Value))
        {
            BindGrid(0);
        }
    }
    protected void gvExamSubject_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(gvExamSubject.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void gvExamSubject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvExamSubject.PageIndex = e.NewPageIndex;
        BindGrid(0);
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int affectedRows = 0;
        for (int i = 0; i < gvExamSubject.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)gvExamSubject.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {

                affectedRows++;
            }
        }
        BindGrid(ListPageContral1.CurrentPageIndex);

    }


    protected void gvExamSubject_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            bool isPermission = false;
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
            {
                isPermission = true;
            }
            else
            {

                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300312"))
                {
                    isPermission = true;
                }

            }
            if (!isPermission)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }

            bool Exit = AptitudeExam.isExitExamScore(Convert.ToInt32(e.CommandArgument));
            if (!Exit)
            {
                AptitudeExam.DeleteAptitudeExam(Convert.ToInt32(e.CommandArgument));
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "删除了一条资质考核题目";
                log.ItemID = e.CommandArgument.ToString();
                log.ItemName = "Combat";
                log.ItemFullUrl = "Aptitude/InsertExam.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
                BindGrid(0);
                Prompt.Text = "提示：共删除1条记录！";
            }
            else
            {
                Prompt.Text = "提示：不可删除,此考核项目已有人员考核！";
            }

        }
    }
}
